﻿<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages.Administration, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" MasterPageFile="~/_admin/popup.master" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="PropertyBagSettings" Namespace="PropertyBagSettings"
    Assembly="PropertyBagSettings, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>


<%--
Property Bag Settings 
By Alon Havivi(havivi@gmail.com, http://havivi.blogspot.com/) 
For full source code and Terms Of Use,
visit http://www.codeplex.com/pbs
--%>
<script runat="server"> 
    private string scope = String.Empty;
    private Guid site = Guid.Empty;
    private Guid web = Guid.Empty;
    private Guid guid = Guid.Empty;
    private string key = String.Empty;
    private string mod = String.Empty;

    const string QS_SCOPE = "scope";
    const string QS_SITE = "site";
    const string QS_WEB = "web";
    const string QS_MODE = "mod";
    const string QS_KEY = "key";
    const string QS_GUID = "guid";
    const string EDIT_HEADER = "<H3 class='ms-standardheader'>Edit Property</H3>";
    const string DELETE_HEADER = "<H3 class='ms-standardheader'>Delete {0} Property</H3>";
    const string ADD_HEADER = "<H3 class='ms-standardheader'>Add Property</H3>";
    const string DEL_MSG = "Delete";
    const string DEL_IMG = "/_layouts/images/DELITEM.GIF";
    const string JS_MSGBOX = "return showMsg('{0}')";
    const string JS_CONFIRM = "return confirmModify('{0}','{1}')";
    const string MSG_DEL = "This will permanently delete the property! Please confirm that you want to delete the property ";
    const string MSG_ADD = "Please confirm that you want to add the property ";
    const string MSG_EDT = "Please confirm that you want to edit the property ";
    const string MSG_ADD_PRO = "The property was added ";
    const string MSG_DEL_PRO = "The property was deleted";
    const string MSG_EDT_PRO = "The property was modified ";
    
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString[QS_SITE] != null && Request.QueryString[QS_SITE] != String.Empty)
        {
            site = new Guid(Request.QueryString[QS_SITE]);
        }
        if (Request.QueryString[QS_WEB] != null && Request.QueryString[QS_WEB] != String.Empty)
        {
            web = new Guid(Request.QueryString[QS_WEB]);
        }
        if (Request.QueryString[QS_GUID] != null && Request.QueryString[QS_GUID] != String.Empty)
        {
            guid = new Guid(Request.QueryString[QS_GUID]);
        }
        if (Request.QueryString[QS_MODE] != null)
        {
            mod = Request.QueryString[QS_MODE];
        }
        if (Request.QueryString[QS_KEY] != null)
        {
            key = Request.QueryString[QS_KEY];
        }
        if (Request.QueryString[QS_SCOPE] != null)
        {
            scope = Request.QueryString[QS_SCOPE];
        }

        if (!Page.IsPostBack)
        {
            switch (mod)
            {
                case "edt":
                    txtKey.Text = key;
                    lblHeader.Text = EDIT_HEADER;
                    txtKey.Enabled = false;

                    LoadProp();
                    lnkSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
                    lnkSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
                    btnSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
                    btnSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
                    break;
                case "del":
                    txtKey.Text = key;
                    lnkSave1.Text = DEL_MSG;
                    lnkSave1.ToolTip = DEL_MSG;
                    lnkSave2.Text = DEL_MSG;
                    lnkSave2.ToolTip = DEL_MSG;
                    btnSave1.ToolTip = DEL_MSG;
                    btnSave2.ToolTip = DEL_MSG;
                    btnSave1.ImageUrl = DEL_IMG;
                    btnSave2.ImageUrl = DEL_IMG;
                    lnkSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
                    lnkSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
                    btnSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
                    btnSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
                    txtKey.Visible = false;
                    cbMask.Visible = false;
                    txtValue.Visible = false;
                    lblKey.Visible = false;
                    lblValue.Visible = false;

                    lblHeader.Text = String.Format(DELETE_HEADER, key);
                    break;
                default:
                    lnkSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
                    lnkSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
                    btnSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
                    btnSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
                    lblHeader.Text = ADD_HEADER;
                    break;
            }

        }

       
    }

    private void LoadProp()
    {
        SPFarm farm;
        farm = SPFarm.Local;
        
        switch (scope)
        {
            case "farm":
                farm = SPFarm.Local;
                if (farm.Properties.ContainsKey(key))
                {
                    txtValue.Text = farm.Properties[key].ToString();
                }
                break;
            case "ser":
                farm = SPFarm.Local;
                SPServer server = farm.Servers[guid];
                if (server.Properties.ContainsKey(key))
                {
                    txtValue.Text = server.Properties[key].ToString();
                }
                break;
            case "app":
                farm = SPFarm.Local;
                SPWebService service = farm.Services.GetValue<SPWebService>("");
                SPWebApplication oSPWebApplication = service.WebApplications[guid];
                if (oSPWebApplication.Properties.ContainsKey(key))
                {
                    txtValue.Text = oSPWebApplication.Properties[key].ToString();
                }
                break;
            case "lst":
                using (SPSite oSPSite = new SPSite(site))
                {
                    using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
                    {
                        SPFolder oSPFolder = oSPWeb.GetFolder(guid);
                        if (oSPFolder.Properties.ContainsKey(key))
                        {
                            txtValue.Text = oSPFolder.Properties[key].ToString();
                        }
                    }
                }
                break;
            default:

                using (SPSite oSPSite = new SPSite(site))
                {
                    using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
                    {
                        if (oSPWeb.AllProperties.ContainsKey(key))
                        {
                            txtValue.Text = oSPWeb.AllProperties[key].ToString();
                        }
                    }
                }
                break;
        }
        if (SPFarm.Local.Properties.ContainsKey("pbs_" + key))
        {
            cbMask.Checked = true;
            txtValue.Enabled = false;
            cbMask.Enabled = false;
            btnSave1.Visible = false;
            btnSave2.Visible = false;
            lnkSave1.Visible = false;
            lnkSave2.Visible = false;
            
            
            txtValue.TextMode = TextBoxMode.Password;
            txtValue.Attributes.Add("value", "*****");
        }
    }
   
    private void Save()
    {
        SPFarm farm;
        farm = SPFarm.Local;
        string pbsValue = txtKey.Text;
        if (cbMask.Checked)
        {
            if (pbsValue != "" && txtKey.Text != "")
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    

                    // instantiate the class with the arrays
                    PropertyBagSettings.cTripleDES des = new PropertyBagSettings.cTripleDES();
                    pbsValue = des.Encrypt(pbsValue);
                    switch (mod)
                    {
                        case "edt":
                            farm.Properties["pbs_" + txtKey.Text] = "*****";
                            farm.Update();
                            lblMsg.Text = MSG_EDT_PRO;
                            break;
                        case "del":

                            farm.Properties["pbs_" + txtKey.Text] = null;
                            farm.Properties.Remove("pbs_" + txtKey.Text);
                            farm.Update();
                            lblMsg.Text = MSG_DEL_PRO;
                            break;
                        default:
                            farm.Properties.Add("pbs_" + txtKey.Text, "*****");
                            farm.Update();
                            lblMsg.Text = MSG_ADD_PRO;
                            break;
                    }
                });
            }  
        }
        switch (scope)
        {
            case "farm":
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    
                    switch (mod)
                    {
                        case "edt":
                            farm.Properties[txtKey.Text] = pbsValue;
                            farm.Update();
                            lblMsg.Text = MSG_EDT_PRO;
                            break;
                        case "del":

                            farm.Properties[txtKey.Text] = null;
                            farm.Properties.Remove(txtKey.Text);
                            farm.Update();
                            lblMsg.Text = MSG_DEL_PRO;
                            break;
                        default:
                            farm.Properties.Add(txtKey.Text, pbsValue);
                            farm.Update();
                            lblMsg.Text = MSG_ADD_PRO;
                            break;
                    }
                });
                break;
            case "ser":
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    farm = SPFarm.Local;
                    SPServer server = farm.Servers[guid];
                    switch (mod)
                    {
                        case "edt":
                            server.Properties[txtKey.Text] = pbsValue;
                            server.Update();
                            lblMsg.Text = MSG_EDT_PRO;
                            break;
                        case "del":

                            server.Properties[txtKey.Text] = null;
                            server.Properties.Remove(txtKey.Text);
                            server.Update();
                            lblMsg.Text = MSG_DEL_PRO;
                            break;
                        default:
                            server.Properties.Add(txtKey.Text, pbsValue);
                            server.Update();
                            lblMsg.Text = MSG_ADD_PRO;
                            break;
                    }
                });
                break;
            case "app":
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    farm = SPFarm.Local;
                    SPWebService service = farm.Services.GetValue<SPWebService>("");
                    SPWebApplication oSPWebApplication = service.WebApplications[guid];

                    switch (mod)
                    {
                        case "edt":
                            oSPWebApplication.Properties[txtKey.Text] = pbsValue;
                            oSPWebApplication.Update();
                            lblMsg.Text = MSG_EDT_PRO;
                            break;
                        case "del":

                            oSPWebApplication.Properties[txtKey.Text] = null;
                            oSPWebApplication.Properties.Remove(txtKey.Text);
                            oSPWebApplication.Update();
                            lblMsg.Text = MSG_DEL_PRO;
                            break;
                        default:
                            oSPWebApplication.Properties.Add(txtKey.Text, pbsValue);
                            oSPWebApplication.Update();
                            lblMsg.Text = MSG_ADD_PRO;
                            break;
                    }
                });
                break;
            case "lst":
                using (SPSite oSPSite = new SPSite(site))
                {
                    using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
                    {
                        SPFolder oSPFolder = oSPWeb.GetFolder(guid);

                        switch (mod)
                        {
                            case "edt":
                                oSPFolder.Properties[txtKey.Text] = pbsValue;
                                SPFolderUpdate(oSPWeb, oSPFolder);
                                //oSPFolder.Update();
                                lblMsg.Text = MSG_EDT_PRO;
                                break;

                            case "del":

                                oSPFolder.Properties[txtKey.Text] = null;
                                oSPFolder.Properties.Remove(txtKey.Text);
                                //oSPFolder.Update();
                                SPFolderUpdate(oSPWeb, oSPFolder);
                                lblMsg.Text = MSG_DEL_PRO;
                                break;

                            default:

                                oSPFolder.Properties.Add(txtKey.Text, pbsValue);
                                //oSPFolder.Update();
                                SPFolderUpdate(oSPWeb, oSPFolder);
                                lblMsg.Text = MSG_ADD_PRO;
                                break;
                        }

                    }
                }
                break;
            default:

                using (SPSite oSPSite = new SPSite(site))
                {
                    using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
                    {
                        switch (mod)
                        {
                            case "edt":
                                oSPWeb.AllProperties[txtKey.Text] = pbsValue;
                                SPWebUpdate(oSPWeb);
                                //oSPWeb.Update();
                                lblMsg.Text = MSG_EDT_PRO;
                                break;
                            case "del":

                                oSPWeb.AllProperties[txtKey.Text] = null;
                                oSPWeb.AllProperties.Remove(txtKey.Text);
                                SPWebUpdate(oSPWeb);
                                //oSPWeb.Update();
                                lblMsg.Text = MSG_DEL_PRO;
                                break;
                            default:

                                oSPWeb.AllProperties.Add(txtKey.Text, pbsValue);
                                SPWebUpdate(oSPWeb);
                                //oSPWeb.Update();
                                lblMsg.Text = MSG_ADD_PRO;
                                break;
                        }
                    }

                }
                break;
        }
      
        if (mod == "del" )
        {
            
            SPSecurity.RunWithElevatedPrivileges( delegate()
                                                      {
                                                          if( farm.Properties.ContainsKey( "pbs_" + txtKey.Text ) )
                                                          {
                                                              farm.Properties[ "pbs_" + txtKey.Text ] = null;
                                                              farm.Properties.Remove( "pbs_" + txtKey.Text );
                                                              farm.Update();
                                                          }
                                                      } );
        }

    }

    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        Save();
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        Save();
    }

    protected void Mask_Clicked(Object sender, EventArgs e)
    {
        if (cbMask.Checked)
        {
            string value = txtValue.Text;
            txtValue.TextMode = TextBoxMode.Password;
            txtValue.Attributes.Add( "value", value );
        }
        else
        {
            txtValue.TextMode = TextBoxMode.SingleLine;
        }

    }


    private void SPWebUpdate(SPWeb oSPWeb)
    {
        if (!oSPWeb.AllowUnsafeUpdates)
        {
            oSPWeb.AllowUnsafeUpdates = true;
            oSPWeb.Update();
            oSPWeb.AllowUnsafeUpdates = false;
        }
        else
        {
            oSPWeb.Update();
        }

    }

    private void SPFolderUpdate(SPWeb oSPWeb, SPFolder oSPFolder)
    {
        if (!oSPWeb.AllowUnsafeUpdates)
        {
            oSPWeb.AllowUnsafeUpdates = true;
            oSPFolder.Update();
            oSPWeb.AllowUnsafeUpdates = false;
        }
        else
        {
            oSPFolder.Update();
        }

    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="server">
  <div>
        <table cellpadding="0" cellspacing="0" style="height: 360; width: 100%">
            <tr>
                <td valign="top" style="height: 10%">
                    <table class="ms-toolbar" width="100%" style="padding-left: 3px; padding-right: 5px;"
                        cellspacing="0" cellpadding="2">
                        <tr>
                            <td width="10%">
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="0" class="ms-toolbar">
                                            <a href="javascript:__doPostBack(&#39;ctl00$PlaceHolderMain$ProfileSave&#39;,&#39;&#39;)"
                                                title="Save and Close">
                                                <asp:ImageButton ID="btnSave1" AlternateText="Save and Close" ImageUrl="/_layouts/images/save.gif"
                                                    BorderWidth="0" runat="server" OnClick="Save_Click" /></a>
                                        </td>
                                        <td class="" width="100%" nowrap>
                                            <asp:LinkButton ID="lnkSave1" runat="server" CssClass="ms-toolbar" ToolTip="Save and Close"
                                                OnClick="Save_Click">Save</asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td class="ms-separator" width="2%">
                                |
                            </td>
                            <td>
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="100%" nowrap>
                                            <a class="ms-toolbar" href="javascript:window.close();">Close</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td valign="top" style="height: 100%;">
                    <table cellpadding="5" cellspacing="5">
                        <tr>
                            <td>
                                <asp:Label ID="lblHeader" runat="server" Text=""></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table>
                                    <tr>
                                        <td class="ms-authoringcontrols">
                                            <asp:Label ID="lblKey" runat="server" CssClass="ms-authoringcontrols" Text=" Key:"></asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtKey" runat="server" Columns="35" MaxLength="260" CssClass="ms-input"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblValue" runat="server" Text=" Value:" CssClass="ms-authoringcontrols"></asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtValue" runat="server" Columns="35" MaxLength="260" CssClass="ms-input"></asp:TextBox> 
                                            <asp:CheckBox ID="cbMask"  OnCheckedChanged="Mask_Clicked" AutoPostBack="true" Text="Masked" runat="server" CssClass="ms-input"></asp:CheckBox>
                                        </td>
                                    </tr>
                                     
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lblMsg"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>           
            <tr>
                <td valign="bottom" style="height: 10%">
                    <table id="Table2" class="ms-toolbar" width="100%" style="padding-left: 3px; padding-right: 5px;"
                        cellspacing="0" cellpadding="2">
                        <tr>
                            <td width="10%">
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="0" class="ms-toolbar">
                                            <a href="javascript:__doPostBack(&#39;ctl00$PlaceHolderMain$ProfileSave&#39;,&#39;&#39;)"
                                                title="Save and Close">
                                                <asp:ImageButton ID="btnSave2" OnClick="Save_Click" AlternateText="Save and Close"
                                                    ImageUrl="/_layouts/images/save.gif" BorderWidth="0" runat="server" /></a>
                                        </td>
                                        <td width="100%" nowrap>
                                            <asp:LinkButton ID="lnkSave2" OnClick="Save_Click" CssClass="ms-toolbar"
                                                runat="server" ToolTip="Save">Save</asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td class="ms-separator" width="1%">
                                |
                            </td>
                            <td>
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="100%" nowrap>
                                            <a class="ms-toolbar" href="javascript:window.close();">Close</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    

    <script language="javascript">
        function confirmModify(key, question) {
            var question = question + key;
            var answer = confirm(question);
            if (answer) {
                return true;
            }
            else if (!answer) {
                return false;
            }

        }
       
    </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Modify Property
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
    runat="server">
    Modify Property
</asp:Content>
